package com.feibi.study.Dialog

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SnapHelper
import com.feibi.study.Adapter.WheelAdapter
import com.feibi.study.Interface.IntCallback
import com.feibi.study.R


class WheelDialog(var context: Context, var title: String, list: MutableList<String>, var intCallback: IntCallback) {
    var rv_wheel: RecyclerView
    lateinit var wheelDialog: BottomDialog
    private var tv_confirm: TextView
    private var tv_title: TextView
    private var tv_cancel: TextView
    val snapHelper = LinearSnapHelper()
    var selectPos = 0

    init {
        val view = View.inflate(context, R.layout.dialog_wheel, null)
        rv_wheel = view.findViewById(R.id.rv_wheel)
        tv_cancel = view.findViewById(R.id.tv_cancel)
        tv_title = view.findViewById(R.id.tv_title)
        tv_confirm = view.findViewById(R.id.tv_confirm)
        tv_confirm.setOnClickListener {
            intCallback.onResult(selectPos)
            wheelDialog.dismiss()
        }
        tv_cancel.setOnClickListener { wheelDialog.dismiss() }
        tv_title.text = title
        if(list.isEmpty()){
            list.add("")
        }
        initData(list)
        wheelDialog = BottomDialog(context, view)
    }

    fun show() {
        wheelDialog.show()
    }

    private fun initData(list: MutableList<String>) {
        snapHelper.attachToRecyclerView(rv_wheel)
        rv_wheel.layoutManager = LinearLayoutManager(context)
        var newList:MutableList<String> = arrayListOf()
        newList.add(0, "")
        newList.add(0, "")
        newList.addAll(list)
        newList.add("")
        newList.add("")
        rv_wheel.adapter = WheelAdapter(context, newList)
        val snapOnScrollListener = SnapOnScrollListener(snapHelper, SnapOnScrollListener.Behavior.NOTIFY_ON_SCROLL_STATE_IDLE, object : OnSnapPositionChangeListener {
            override fun onSnapPositionChange(position: Int) {
                selectPos = position - 2
                selectPos = if (selectPos < 0) 0 else selectPos
            }
        })
        rv_wheel.addOnScrollListener(snapOnScrollListener)
    }

    class SnapOnScrollListener(
        private val snapHelper: SnapHelper,
        var behavior: Behavior = Behavior.NOTIFY_ON_SCROLL,
        var onSnapPositionChangeListener: OnSnapPositionChangeListener? = null
    ) : RecyclerView.OnScrollListener() {
        enum class Behavior {
            NOTIFY_ON_SCROLL,
            NOTIFY_ON_SCROLL_STATE_IDLE
        }

        private var snapPosition = RecyclerView.NO_POSITION

        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            if (behavior == Behavior.NOTIFY_ON_SCROLL) {
                maybeNotifySnapPositionChange(recyclerView)
            }
        }

        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            if (behavior == Behavior.NOTIFY_ON_SCROLL_STATE_IDLE
                && newState == RecyclerView.SCROLL_STATE_IDLE
            ) {
                maybeNotifySnapPositionChange(recyclerView)
            }
        }

        private fun maybeNotifySnapPositionChange(recyclerView: RecyclerView) {
            val snapPosition = snapHelper.getSnapPosition(recyclerView)
            val snapPositionChanged = this.snapPosition != snapPosition
            if (snapPositionChanged) {
                onSnapPositionChangeListener?.onSnapPositionChange(snapPosition)
                this.snapPosition = snapPosition
            }
        }

        fun SnapHelper.getSnapPosition(recyclerView: RecyclerView): Int {
            val layoutManager = recyclerView.layoutManager ?: return RecyclerView.NO_POSITION
            val snapView = findSnapView(layoutManager) ?: return RecyclerView.NO_POSITION
            return layoutManager.getPosition(snapView)
        }

    }

    interface OnSnapPositionChangeListener {
        fun onSnapPositionChange(position: Int)
    }
}